﻿using Dapr;
using Dapr.Actors;
using Dapr.Actors.Client;
using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using MyActor.Interfaces;
using MyActorService.Models;
using System.Diagnostics;

namespace MyActorService.Controllers
{
    public class BindingController : Controller
    {
        private readonly ILogger<StateController> _logger;
        private readonly DaprClient daprclient;
        public BindingController(ILogger<StateController> logger, DaprClient daprClient)
        {
            _logger = logger;
            daprclient = daprClient;
        }



        /// <summary>
        /// 写入mysql数据库
        /// </summary>
        /// <returns></returns>
        public async Task<string> Sql()
        {
            try
            {
                var metadata =
                    new Dictionary<string, string>
                    {
                        ["sql"] = $"INSERT INTO  `users` (`id`, `name`, `age`) VALUES (18, '张胜1', 12);",
                    };
                await daprclient.InvokeBindingAsync("mysql", "exec", new { }, metadata);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
            }
            return $"{DateTime.Now}";
        }





    }
}